package de.fhbrs.fedora;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

import java.util.List;
import java.util.Vector;

public class GSearchParser {
	private Document xmlDoc = null;
	private List<GSearchObject> objects = null;
	
	public GSearchParser(String gSearchResult) throws Exception {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		factory.setNamespaceAware(true);
		DocumentBuilder builder = factory.newDocumentBuilder();
		InputStream inputStream = new ByteArrayInputStream(gSearchResult.getBytes("UTF-8"));
		this.xmlDoc = builder.parse(inputStream);
		
		if(getValueForAttribute("hitTotal")>=1){
			this.objects = new Vector<GSearchObject>();
			NodeList objects = getNodeObjects();
			for(int a=1;a<objects.getLength();a+=2){
				GSearchObject object = new GSearchObject();
				NodeList metadata = objects.item(a).getChildNodes();
				for(int b=1;b<metadata.getLength();b+=2){
					NamedNodeMap attribute = metadata.item(b).getAttributes();
					Node name = attribute.getNamedItem("name");
					if(name.getNodeValue().compareTo("PID")==0){
						object.setPid(metadata.item(b).getTextContent());
					}
					if(name.getNodeValue().compareTo("dc.title")==0){
						if(metadata.item(b).getTextContent().startsWith("title1=", 0)){
							if(metadata.item(b).getTextContent().length() >= 8){
								object.setSignatur(metadata.item(b).getTextContent().substring(8, metadata.item(b).getTextContent().length()));
							}
						}
						if(metadata.item(b).getTextContent().startsWith("title2=", 0)){
							if(metadata.item(b).getTextContent().length() >= 8){
								object.setSignatur(metadata.item(b).getTextContent().substring(8, metadata.item(b).getTextContent().length()));
							}
						}
					}
					if(name.getNodeValue().compareTo("dc.creator")==0){
						object.setAuthor(Utility.convertToArray(metadata.item(b).getTextContent()));
					}
					if(name.getNodeValue().compareTo("dc.description")==0){
						if(metadata.item(b).getTextContent().startsWith("signatur=", 0)) {
							if(metadata.item(b).getTextContent().length() >= 10){
								object.setSignatur(metadata.item(b).getTextContent().substring(10, metadata.item(b).getTextContent().length()));
							}
						}
						if(metadata.item(b).getTextContent().startsWith("institute=", 0))
							if(metadata.item(b).getTextContent().length() >= 11){
								object.setSignatur(metadata.item(b).getTextContent().substring(11, metadata.item(b).getTextContent().length()));
							}
						if(metadata.item(b).getTextContent().startsWith("shelf=", 0))
							if(metadata.item(b).getTextContent().length() >= 7){
								object.setSignatur(metadata.item(b).getTextContent().substring(7, metadata.item(b).getTextContent().length()));
							}
						if(metadata.item(b).getTextContent().startsWith("vol=", 0))
							if(metadata.item(b).getTextContent().length() >= 5){
								object.setSignatur(metadata.item(b).getTextContent().substring(5, metadata.item(b).getTextContent().length()));
							}
						if(metadata.item(b).getTextContent().startsWith("page=", 0))
							if(metadata.item(b).getTextContent().length() >= 6){
								object.setSignatur(metadata.item(b).getTextContent().substring(6, metadata.item(b).getTextContent().length()));
							}
						if(metadata.item(b).getTextContent().startsWith("journal=", 0))
							if(metadata.item(b).getTextContent().length() >= 9){
								object.setSignatur(metadata.item(b).getTextContent().substring(9, metadata.item(b).getTextContent().length()));
							}
						if(metadata.item(b).getTextContent().startsWith("inventorynumber=", 0))
							if(metadata.item(b).getTextContent().length() >= 17){
								object.setSignatur(metadata.item(b).getTextContent().substring(17, metadata.item(b).getTextContent().length()));
							}
					}
					if(name.getNodeValue().compareTo("dc.date")==0){
						object.setYear(metadata.item(b).getTextContent());
					}
					if(name.getNodeValue().compareTo("dc.type")==0){
						object.setLitart(metadata.item(b).getTextContent());
					}
					if(name.getNodeValue().compareTo("dc.identifier")==0){
						if(metadata.item(b).getTextContent().startsWith("ISBN=", 0)) {
							if(metadata.item(b).getTextContent().length() >= 6){
								object.setSignatur(metadata.item(b).getTextContent().substring(6, metadata.item(b).getTextContent().length()));
							}
						}
						if(metadata.item(b).getTextContent().startsWith("ISSN=", 0)) {
							if(metadata.item(b).getTextContent().length() >= 6){
								object.setSignatur(metadata.item(b).getTextContent().substring(6, metadata.item(b).getTextContent().length()));
							}
						}
					}
					if(name.getNodeValue().compareTo("dc.subject")==0){
						object.setRemarks(metadata.item(b).getTextContent());
					}
					if(name.getNodeValue().compareTo("dc.contibutor")==0){
						object.setEditor(metadata.item(b).getTextContent());
					}
					if(name.getNodeValue().compareTo("dc.source")==0){
						object.setAddress(metadata.item(b).getTextContent());
					}
					if(name.getNodeValue().compareTo("dc.publisher")==0){
						object.setPublisher(metadata.item(b).getTextContent());
					}
					if(name.getNodeValue().compareTo("dc.coverage")==0){
						object.setRoom(metadata.item(b).getTextContent());
					}
				}
				this.objects.add(object);
			}
		} 
	}
	
	public List<GSearchObject> getObjects(){
		return this.objects;
	}
	
	private int getValueForAttribute(String name){
		Element root = this.xmlDoc.getDocumentElement();
		NodeList metadata = root.getChildNodes();
		for(int a=1;a<metadata.getLength();a+=2){
			if(metadata.item(a).getNodeName().compareTo("gfindObjects")==0){
				NamedNodeMap attributes = metadata.item(a).getAttributes();
				Node hitTotal = attributes.getNamedItem(name);
				return Integer.parseInt(hitTotal.getNodeValue());				
			}
		}
		return 0;
	}
	
	private NodeList getNodeObjects(){
		Element root = this.xmlDoc.getDocumentElement();
		NodeList gfindObjects = root.getChildNodes();
		NodeList objects = gfindObjects.item(1).getChildNodes();
		return objects.item(1).getChildNodes();
	}
}
